<?php
/**
 * Joomla! 1.5 component NS Pro
 *
 * @version $Id: nspro.php 2009-04-28 01:47:12 svn $
 * @author Christopher Mavros
 * @package Joomla
 * @subpackage NS Pro
 * @license GNU/GPL
 *
 */

// no direct access
defined('_JEXEC') or die('Restricted access');

// Import Joomla! libraries
jimport('joomla.application.component.model');

class NsproModelNewsletteredit extends JModel {
    function __construct()
    {
        parent::__construct();

        $array = JRequest::getVar('cid',  0, '', 'array');
        $this->setId((int)$array[0]);
    }
    
    function setId($id)
    {
        // Set id and wipe data
        $this->_id      = $id;
        $this->_data    = null;
    }

    function getNewsletter()
    {
        $db =& JFactory::getDBO();
        // Load the data
        if (empty( $this->_data )) {
            $query = ' SELECT * FROM `#__nspro_newsletters` '.
                    '  WHERE `id` = '.$this->_id;
            $db->setQuery( $query );
            $this->_data = $db->loadObject();
        }
        if (!$this->_data) {
            $this->_data = new stdClass();
            $this->_data->id = 0;
            $this->_data->fName  = null;
            $this->_data->fEmail = null;
            $this->_data->subject = null;
            $this->_data->message = null;
            $this->_data->datetime = null;
            $this->_data->sent = false;
            $this->_data->mailing_lists = null;
            $this->_data->emails_per_send = 0;
            $this->_data->LastID = 0;
            $this->_data->is_scheduled = 0;
            $this->_data->scheduledatetime = null;
        }
        return $this->_data;
    }

    function getNewsletters($limit, $limitstart) {
        $db =& JFactory::getDBO();

        $query = 'SELECT * FROM `#__nspro_newsletters` ORDER BY `id`';

        $newsletterss = $this->_getList( $query , $limitstart, $limit);

        return $newsletterss;
    }

    function getNewslettersCount() {
        $db =& JFactory::getDBO();

        $query = 'SELECT COUNT(*) FROM `#__nspro_newsletters`';

        $db->setQuery( $query );
        $nCount = $db->loadResult();

        return $nCount;
    }

    function store()
    {
        $db =& JFactory::getDBO();
        $row =& $this->getTable('nsnewsletters');
        //$data = JRequest::get( 'post' );
        $data = $_POST;
        $data['fName'] = stripcslashes($data['fName']);
        $data['subject'] = stripcslashes($data['subject']);        
        $data['message'] = stripcslashes($data['message']);
        $data["scheduledatetime"] = date('Y-m-d H:i:s', strtotime($data["scheduledatetime"]));        

        // Bind the form fields to the nsnewsletters table
        if (!$row->bind($data)) {
            $this->setError($db->getErrorMsg());
            return false;
        }

        // Make sure the nsnewsletters records are valid
        if (!$row->check()) {
            $this->setError($db->getErrorMsg());
            return false;
        }

        // Store the nsnewsletters table to the database
        if (!$row->store()) {
            $this->setError( $row->getErrorMsg() );
            return false;
        }

        return true;
    }

    function delete()
    {
        $cids = JRequest::getVar( 'cid', array(0), 'post', 'array' );

        $row =& $this->getTable('nsnewsletters');

        if (count( $cids ))
        {
            foreach($cids as $cid) {
                if (!$row->delete( $cid )) {
                    $this->setError( $row->getErrorMsg() );
                    return false;
                }
                else {
                  $db =& JFactory::getDBO();
                  
                  $db->setQuery('DELETE FROM `#__newsletter_openings` WHERE `nid` = "'.mysql_escape_string($cid).'"');
                  $db->query();
                  
                  $db->setQuery('DELETE FROM `#__newsletter_attachments` WHERE `newsletter_id` = "'.mysql_escape_string($cid).'"');
                  $db->query();
                }
            }
        }
        return true;
    }

}
?>